document.ready(function () {
    //选项卡切换

    //获取元素
    let navList = document.querySelector('.modal .navList');
    let navListSpan = navList.querySelectorAll('.navList span');
    let navListContent = document.querySelectorAll('.modal .navListContent');

    //调用选项卡函数并传参数
    tabCard(navList, 'SPAN', navListSpan, 'active', navListContent, 'on');
    // 用户账号和密码验证   

    // 获取元素
    let userName = document.querySelector('.userName');
    let userPwd = document.querySelector('.userPwd');
    let delBtn = document.querySelectorAll('.delBtn');
    let loginBtn = document.querySelector('.loginBtn');
    let errInfo = document.querySelector('.errInfo');
    let errImg = document.querySelector('.errImg');

    // userName框输入值长度大于零时,显示delBtn按钮,反之隐藏
    userName.addEventListener('input', function () {
        if (this.value) {
            delBtn[0].style.display = 'block';
        } else {
            delBtn[0].style.display = 'none';
        }
    })

    //userPwd框输入值长度大于零时,显示delBtn按钮,反之隐藏
    userPwd.addEventListener('input', function () {
        if (this.value) {
            delBtn[1].style.display = 'block';
        } else {
            delBtn[1].style.display = 'none';
        }
    })

    // 点击delBtn按钮,清空输入框值,并因此自己
    delBtn.forEach(function (v) {
        v.addEventListener('click', function () {
            v.previousElementSibling.value = '';
            v.style.display = 'none';
        })
    })

    // 点击登录按钮,验证用户名和密码
    loginBtn.addEventListener('click', function () {
        // 获取用户名得输入值
        let nameValue = userName.value;
        // 获取密码框得输入值
        let pwdValue = userPwd.value;
        let num = 0;
        let timer1;
        let timer2;
        function showErr() {
            if (num == 1) {
                errInfo.style.display = 'none';
                clearInterval(timer1);
                return
            }
            errInfo.style.display = 'block';
            errImg.textContent = '用户名格式不正确';
            ++num;
        }
        if ((nameValue.length < 6) || (nameValue.length > 12)) {
            // clearInterval(timer1)
            showErr();
            // errInfo.style.display = 'block';
            timer1 = setInterval(function () {
                if (num == 1) {
                    errInfo.style.display = 'none';
                    clearInterval(timer1);
                    return;
                }
                errInfo.style.display = 'block';
                errImg.textContent = '用户名格式不正确';
                ++num;
            }, 500);

        } else if ((pwdValue.length < 6) || (pwdValue.length > 12)) {
            // errInfo.style.display = 'block';
            timer2 = setInterval(function () {
                if (num == 1) {
                    errInfo.style.display = 'none';
                    clearInterval(timer2);
                    return;
                }
                errInfo.style.display = 'block';
                errImg.textContent = '密码格式不正确';
                ++num;
            }, 500);

        } else {
            location.href = './home.html';
            userName.value = '';
        }
    })
})